<?php

namespace App\Http\Controllers\Api;

use App\Models\ShopCar;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ShopcarController extends Controller
{
    public function create(Request $request)
    {
        $num = $request->get('num');
        if($num<1 || $num==null || $num==""){
            $num=1;
        }
        if($shopcar = ShopCar::where([
            ['user_id',$request->get('user')->user_id],
            ['goods_id',$request->get('goods_id')]
        ])->first()){
            $shopcar->num+=$num;
            $shopcar->save();
            return ['status_code'=>1,'message'=>"添加成功"];
        }else{
            $data = [
                'user_id'=>$request->get('user')->user_id,
                'goods_id'=>$request->get('goods_id'),
                'num'=>$num,
            ];
            ShopCar::create($data);
            return ['status_code'=>1,'message'=>"添加成功"];
        }
    }

    public function lists(Request $request)
    {
        $shopcar = ShopCar::where('user_id',$request->get('user')->user_id)->get();
        foreach ($shopcar as $value){
            $my_goods = $value->my_goods;
            $cover = $my_goods->my_picture()->where('type',1)->first()->my_pic_path->pic_path;
            $my_goods->cover = $cover;
        }
        return ['status_code','data'=>$shopcar];
    }

    public function destory(Request $request)
    {
        return ShopCar::destroy($request->get('shop_car_id'))?
            ['status_code'=>1,'message'=>"删除成功"]:
            ['status_code'=>0,'message'=>"失败"];
    }

    public function num(Request $request)
    {
        $shopcar =  ShopCar::find($request->get('shop_car_id'));
        $num=$request->get('num');
        if($num<1 || $num==null || $num==""){
            $num=1;
        }
        $shopcar->num = $num;
        return $shopcar->save()?
            ['status_code'=>1,'message'=>"成功"]:
            ['status_code'=>0,'message'=>"失败"];
    }
}
